package com.ruicar.afs.cloud.afscase.approvevisit.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.afscase.approvevisit.entity.ApproveFieldVisitInfo;
import com.ruicar.afs.cloud.afscase.approvevisit.service.ApproveFieldVisitInfoService;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @Description
 * @Author Peng_Jinbo
 * @Date 2020/06/12 13:51
 */
@RestController
@RequestMapping("/approveVisit")
@Api("家访信息")
@AllArgsConstructor
public class ApproveFieldVisitInfoController {

    ApproveFieldVisitInfoService approveFieldVisitInfoService;

    @GetMapping("/visitLevel")
    @ApiOperation("家访评分查询")
    public IResponse queryVisitLevel(@ModelAttribute ApproveFieldVisitInfo approveFieldVisitInfo) {
        if (Objects.nonNull(approveFieldVisitInfo)) {
            return IResponse.success(approveFieldVisitInfoService.getOne(Wrappers.<ApproveFieldVisitInfo>lambdaQuery()
                    .eq(StrUtil.isNotBlank(approveFieldVisitInfo.getApplyNo()), ApproveFieldVisitInfo::getApplyNo, approveFieldVisitInfo.getApplyNo())
                    .eq(StrUtil.isNotBlank(approveFieldVisitInfo.getStageId()), ApproveFieldVisitInfo::getStageId, approveFieldVisitInfo.getStageId())));
        } else {
            return IResponse.success(new ApproveFieldVisitInfo());
        }
    }

    @PostMapping("/saveVisitLevel")
    @ApiOperation("家访评分保存")
    @Transactional
    public IResponse saveVisitLevel(@RequestBody ApproveFieldVisitInfo approveFieldVisitInfo) {
        if (Objects.nonNull(approveFieldVisitInfo)) {
            if (Objects.isNull(approveFieldVisitInfo.getId())) {
                approveFieldVisitInfoService.save(approveFieldVisitInfo);
            } else {
                approveFieldVisitInfoService.updateById(approveFieldVisitInfo);
            }
        }
        return IResponse.success("success");
    }

}
